<template>
    <div class="container">
        <!-- header -->
        <div class="header">
            <p class="header-title">选择学校</p>
            <el-select
                v-model="school"
                filterable
                placeholder="请选择"
                size="small"
                @change="getTableData(1)"
            >
                <el-option
                    v-for="item in optionSchool"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
            </el-select>
            <div class="header-right">
                <el-input
                    v-model="tel"
                    placeholder="请输入用户手机号"
                    suffix-icon="el-icon-search"
                    size="small"
                    class="cont-ipt"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
                <el-input
                    v-model="cont"
                    placeholder="请输入动态文字"
                    suffix-icon="el-icon-search"
                    size="small"
                    class="cont-ipt"
                    clearable
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                label="ID"
                width="100px"
                type="index"
                align="center"
            />
            <el-table-column
                prop="student.school"
                label="学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student.true_name"
                label="用户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student.phone"
                label="用户手机号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="post_content"
                label="动态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span
                        class="comment-cont"
                        @click="fnCont(scope.row)"
                    >{{ scope.row.post_content }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="content"
                label="评论内容"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="create_time"
                label="评论时间"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span>{{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.is_delete == 0 && scope.row.status == 1">显示</span>
                    <span v-if="scope.row.is_delete == 0 && scope.row.status == 0">隐藏</span>
                    <span v-if="scope.row.is_delete == 1">已删除</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                fixed="right"
                :width="250"
            >
                <template slot-scope="scope">
                    <span
                        class="curse-ford"
                        @click="fnForbidden(scope.row)"
                    >禁言用户</span>
                    <span
                        class="curse-ford"
                        @click="fnSetStatus(scope.row)"
                        v-if="scope.row.is_delete == 0 && scope.row.status == 1"
                    >隐藏</span>
                    <span
                        class="curse-ford"
                        @click="fnSetStatus(scope.row)"
                        v-if="scope.row.is_delete == 0 && scope.row.status == 0"
                    >显示</span>
                    <span
                        class="curse-ford"
                        @click="fnRecovery(scope.row)"
                        v-if="scope.row.is_delete == 1"
                    >恢复</span>
                    <span
                        class="curse-ford"
                        @click="fnRecovery(scope.row)"
                        v-if="scope.row.is_delete == 0"
                    >删除</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- dialog -->
        <el-dialog
            title="请选择禁言时间"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="showForbidden"
            center
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForbidden"
            >
                <el-form-item
                    prop='forbidden_time'
                >
                    <el-radio-group v-model="ruleForm.forbidden_time">
                        <el-radio label="1">1天</el-radio>
                        <el-radio label="2">2天</el-radio>
                        <el-radio label="3">3天</el-radio>
                        <el-radio label="7">7天</el-radio>
                        <el-radio label="0">永久禁言</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    type="primary"
                    @click="submitForbidden('ruleForbidden')">确定</el-button>
            </div>
        </el-dialog>
        <!-- dialog -->

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->
    </div>
</template>

<script>
export default {
    data () {
        return {
            school: '',
            optionSchool: [{
                value: '',
                label: '全部'
            }],
            tel: '',
            cont: '',
            post_no: '',
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            showForbidden: false,
            ruleForm: {
                forbidden_time: ''
            },
            forbid: {
                student_no: '',
                forbid: '1',
                day: ''
            },
            forum_no: '',
            comment_no: '',
            rules: {
                forbidden_time: [
                    { required: true, message: '请选择禁言时间', trigger: 'blur' }
                ]
            }
        };
    },
    created () {
        const arg = this.$route.query;

        if (arg['post_no'] !== '' || arg['comment_no'] !== '' || arg['content'] !== '') {
            this.post_no = arg['post_no'];
            this.cont = arg['content'];
            this.comment_no = arg['comment_no'];
        } else {
            this.$message({
                type: 'warning',
                message: '获取评论列表失败'
            });
        }

        this.setHeader();
        this.getSchoolList();
        this.getTableData();

    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '社交'
            },
            {
                title: "评论列表"
            }]
            );
        },

        /**
         * 获取已开通学校列表
         */
        getSchoolList () {
            this.$post('/student/forum%5Cget_school_list', {
            }, resp => {
                if (resp.code == 1) {
                    let list = [];

                    resp.data.list.map(item => {
                        list.push({
                            value: item.forum_no,
                            label: item.school_name
                        });
                    });

                    this.optionSchool = this.optionSchool.concat(list);
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/forum_post_comment%5Cget_list', {
                page_id: page || this.page.now_page,
                post_no: this.post_no,
                forum_no: this.school,
                student_phone: this.tel,
                content: this.cont,
                comment_no: this.comment_no
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 确定禁言
         */
        submitForbidden(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.forbid['day'] = this.ruleForm['forbidden_time'];
                    this.$post('/student/forum_student%5Cforbid', {
                        student_no: this.forbid['student_no'],
                        forbid: this.forbid['forbid'],
                        day: this.forbid['day'],
                        forum_no: this.school == '' ? this.forum_no : this.school
                    }, resp => {
                        this.showForbidden = !this.showForbidden;
                        if (resp.code == 1) {
                            this.$message({
                                type: 'success',
                                message: '操作成功!'
                            });
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'warning',
                                message: resp.msg
                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 禁言用户
         */
        fnForbidden (row) {
            let school_name = row.student.school;

            if (this.school == '') {
                this.optionSchool.map((item,index)=>{
                    if(item.label == school_name) {
                        this.forum_no = item.value;
                    }
                    return;
                });
            }

            this.showForbidden = !this.showForbidden;
            this.forbid['student_no'] = row.student['student_no'];
        },

        /**
         * 隐藏/显示
         */
        fnSetStatus (row) {
            let status = row.status;
            let comment_no = row.comment_no;

            status = status == 1 ? status = 0 : status = 1;

            this.$post('/student/forum_post_comment%5Cset_status', {
                comment_no: comment_no,
                status: status,
            }, resp => {
                if (resp.code == 1) {
                    this.$set(row, 'status', status);
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 删除/恢复
         */
        fnRecovery (row) {
            let is_delete = row.is_delete;
            let comment_no = row.comment_no;

            is_delete = is_delete == 0 ? is_delete = 1 : is_delete = 0;

            this.$post('/student/forum_post_comment%5Cdelete', {
                comment_no: comment_no,
                delete: is_delete,
            }, resp => {
                if (resp.code == 1) {
                    this.$set(row, 'is_delete', is_delete);
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 跳转内容列表列表
         */
        fnCont (row) {
            const content = row.post_content;

            this.$router.replace({
                name: 'SocialCont',
                query: {
                    content
                }
            });
        },
        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        .header {
            margin-bottom: 20px;
            position: relative;
            display: flex;
            align-items: center;
            .header-title {
                color: #606266;
                margin-right: 8px;
                white-space: nowrap;
            }
            .el-input{
                width: 200px;
            }
            .header-right {
                display: flex;
                margin-left: 30px;
                align-items: center;
                .cont-ipt {
                    margin-left: 20px;
                }
            }
        }
        .curse-ford , .comment-cont{
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
</style>
<style lang="less">
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
</style>